package com.xmotor.test_lottery;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 测试
 * @Author Dang Xianyue
 * @Date 2020-11-08 20:43
 * @Version 1.0
 */
public class Test {
    public static void main(String[] args) {
        List<Goods> goodsList = new ArrayList<Goods>(){
            {
                add(new Goods().setId("01").setName("一等奖").setWeight(new BigDecimal("0.5")));
                add(new Goods().setId("02").setName("二等奖").setWeight(new BigDecimal("2.5")));
                add(new Goods().setId("03").setName("三等奖").setWeight(new BigDecimal("3")));
                add(new Goods().setId("04").setName("谢谢惠顾").setWeight(new BigDecimal("4")));
            }
        };

        List<Double> orignalRates = new ArrayList<>();
        for (int i = 0; i < goodsList.size(); i++) {
            Goods goods = goodsList.get(i);
            orignalRates.add(goods.getWeight() != null ? goods.getWeight().doubleValue() : 0d);
        }


        // 统计
        Map<Integer, Integer> count = new HashMap<>();

        // 测试次数
        double num = 1000000;
        for (int i = 0; i < num; i++) {
            int orignalIndex = LotteryUtils.lottery(orignalRates);
            Integer value = count.get(orignalIndex);
            count.put(orignalIndex, value == null ? 1 : value + 1);
        }

        for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
            System.out.println(goodsList.get(entry.getKey()) + ", 命中次数=" + entry.getValue() + ", 实际概率="
                    + entry.getValue() / num);
        }
    }
}
